package com.example.doufacedemo;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;

import com.ocom.ocomdevicesdk.utils.LogHelper;

import java.util.HashMap;
import java.util.Iterator;

public   class UsbPreUntil {
    private static UsbManager mUsbManager;
    private static Context mContext;
    private static UsbDevice mUsbDevice;
    private static UsbDeviceConnection mUsbDeviceConnection;
    private static HashMap<String, UsbEndpoint> mUsbEndpointOutMap = new HashMap();
    private static HashMap<String, UsbEndpoint> mUsbEndpointInMap = new HashMap();
    @SuppressLint({"WrongConstant"})
    public static void init(Context context) {
        mContext = context.getApplicationContext();
        mUsbManager = (UsbManager)mContext.getSystemService("usb");
    }
    public static int requestPermisstion(int pid, int vid) {

        if (mUsbManager == null) {
            LogHelper.unlimitedPrintSendMsg("UsbPreUntil", "UsbManager为Null 请确认传入Context");
            return 1;
        } else {
            HashMap<String, UsbDevice> deviceMap = mUsbManager.getDeviceList();
            Iterator var1 = deviceMap.values().iterator();
            LogHelper.unlimitedPrintSendMsg("UsbPreUntil", "开始搜索usb设备....：");

            UsbDevice device;
            do {
                if (!var1.hasNext()) {
                    LogHelper.unlimitedPrintSendMsg("UsbPreUntil", "搜索设备结束，没搜索到设备....：");
                    return 1;
                }

                device = (UsbDevice)var1.next();
                LogHelper.unlimitedPrintSendMsg("UsbPreUntil", "搜索到usb设备：" + device.toString());
            } while(device.getVendorId() != vid || device.getProductId() != pid);

            LogHelper.unlimitedPrintSendMsg("UsbPreUntil", "发现目标usb设备：" + device.toString());
            mUsbDevice = device;
            if (mUsbManager.hasPermission(mUsbDevice)) {
                return usbDeviceInit(mUsbDevice);
            } else {
                PendingIntent mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("com.android.example.USB_PERMISSION"), 0);
                mUsbManager.requestPermission(mUsbDevice, mPermissionIntent);
                return 2;
            }
        }
    }

    private static int usbDeviceInit(UsbDevice device) {
        int interfaceCount = device.getInterfaceCount();
        UsbInterface usbInterface = null;

        int j;
        for(j = 0; j < interfaceCount; ++j) {
            usbInterface = device.getInterface(j);
            if (usbInterface.getInterfaceClass() == 7) {
                break;
            }
        }

        if (usbInterface != null) {
            mUsbDeviceConnection = mUsbManager.openDevice(device);
            if (mUsbDeviceConnection != null) {
                if (mUsbDeviceConnection.claimInterface(usbInterface, true)) {
                    for(j = 0; j < usbInterface.getEndpointCount(); ++j) {
                        UsbEndpoint endpoint = usbInterface.getEndpoint(j);
                        if (endpoint.getDirection() == 0) {
                            mUsbEndpointOutMap.put(device.getDeviceName(), endpoint);
                        } else {
                            mUsbEndpointInMap.put(device.getDeviceName(), endpoint);
                        }
                    }

                    if (mUsbEndpointInMap.size() != 0 && mUsbEndpointOutMap.size() != 0) {
                        return 0;
                    } else {
                        mUsbEndpointInMap.clear();
                        mUsbEndpointOutMap.clear();
                        mUsbDeviceConnection.close();
                        return 3;
                    }
                } else {
                    return 3;
                }
            } else {
                return 3;
            }
        } else {
            return 3;
        }
    }
}
